<template>
    <div class="user-header">
        <el-button type="primary" @click="dialogVisible = true">新增</el-button>
        <el-form :inline="true" :model="formInline">
            <el-form-item label="请输入">
                <el-input v-model="formInline.keyword" placeholder="请输入用户名" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handelSearch">搜索</el-button>
            </el-form-item>
        </el-form>
    </div>
    <!-- 用户列表 -->
    <div class="table-box">
        <el-table :data="list" style="width: 100%;" height="500px">
            <el-table-column v-for="item in tableLabel" :key="item.prop" :label="item.label" :prop="item.prop"
                :width="item.width ? item.width : 125">
            </el-table-column>
            <!-- 操作 -->
            <el-table-column label="操作" min-width="180">
                <template #default>
                    <el-button size="small">编辑</el-button>
                    <el-button size="small" type="danger">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination layout="prev, pager, next" :total="config.total" @current-change="chagePage" />
    </div>
    <!-- 新增对话框 -->
    <el-dialog v-model="dialogVisible" title="新增" width="500" :before-close="handleClose">
        <!-- 表单 -->
        <el-form :inline="true" :model="formUser" class="demo-form-inline">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="姓名" prop="name" :rules="[{ required: true, message: '姓名是必填项', }]">
                        <el-input v-model="formUser.name" placeholder="请输入姓名" clearable />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="年龄" prop="age" :rules="[{ required: true, message: '年龄是必填项', },{ type: 'number', message: '年龄必须是数字' }]">
                        <el-input v-model="formUser.age" placeholder="请输入年龄" clearable />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="性别" prop="sex" style="width: 190px;" :rules="[{ required: true, message: '性别是必填项', }]">
                        <el-select v-model="formUser.sex" placeholder="请选择" clearable>
                            <el-option label="男" value="0" />
                            <el-option label="女" value="1" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="出生日期" prop="birth" :rules="[{ required: true, message: '出生日期是必填项', }]">
                        <el-date-picker v-model="formUser.birth" type="date" placeholder="请输入" clearable />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-form-item label="地址" prop="addr" :rules="[{ required: true, message: '地址是必填项', }]">
                    <el-input v-model="formUser.addr" placeholder="请输入地址"></el-input>
                </el-form-item>
            </el-row>
            <el-row style="justify-content: flex-end;">
                <el-form-item>
                    <el-button type="primary" @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="dialogVisible = false">确定</el-button>
                </el-form-item>
            </el-row>
        </el-form>
    </el-dialog>


</template>


<script setup>
import { getCurrentInstance, onMounted, reactive, ref } from 'vue'
const { proxy } = getCurrentInstance()
const tableLabel = reactive([
    {
        prop: 'name',
        label: '姓名'
    },
    {
        prop: 'age',
        label: '年龄'
    },
    {
        prop: 'sexLabel',
        label: '性别'
    },
    {
        prop: 'birth',
        label: '出生日期',
        width: 200,
    },
    {
        prop: 'addr',
        label: '地址',
        width: 200,
    },
])
// 分页器数据
let config = reactive({
    total: 0,
    page: 1,
    name: ''
})
// 页码数变化
const chagePage = (page) => {
    config.page = page
    getUserData(config)
}
// 搜索关键词
const formInline = reactive({
    keyword: ''
})
// 搜索的功能
const handelSearch = () => {
    config.name = formInline.keyword
    getUserData(config)
}
//控制对话框显示
const dialogVisible = ref(false)

const handleClose = (done) => {
    ElMessageBox.confirm('是否确定关闭？')
        .then(() => {
            done()
        })
        .catch(() => {
            // catch error
        })
}

// 添加用户的数据
const formUser = reactive({
    name: '',
    age: '',
    sex: '',
    birth: '',
    addr: ''
})

let list = ref([])
// 获取用户表格数据
const getUserData = async (config) => {
    let res = await proxy.$api.getUserDataL(config)
    // 总页数
    config.total = res.count
    // console.log(res.count);
    list.value = res.list.map((item) => {
        item.sexLabel = item.sex === 0 ? '男' : '女'
        return item
    })
    // console.log(res);

}
onMounted(() => {
    // 获取用户表格数据
    getUserData(config)
})

</script>


<style>
.user-header {
    display: flex;
    justify-content: space-between;
}
.el-dialog{
width: 600px;
}
</style>